﻿Ext.BLANK_IMAGE_URL = '../Scripts/ext/resources/images/default/s.gif';
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'title';
var ALL_PAGESIZE_SETTING = 5;
var CurrentUserStore = new Ext.data.JsonStore({
	// store configs
	autoDestroy: true,
	url: '/InfraHospUser/GetCurrentUser',
	storeId: 'CurrentUserStore',
	root: 'data',
	totalProperty: 'total',
	fields: [{ name: 'Id', type: 'string' },
			{ name: 'Name', type: 'string' },
			{ name: 'LoginName', type: 'string' },
			{ name: 'Code', type: 'string' },
			{ name: 'DepartmentId', type: 'string' },
			{ name: 'UserGroup', type: 'string' },
			{ name: 'State', type: 'string' }
		]
});

var CurrentUserMenuPermission = new Ext.data.JsonStore({
	// store configs
	autoDestroy: true,
	url: '/InfraHospUser/GetCurrentUserMenuPermission',
	storeId: 'CurrentUserMenuPermissionStore',
	root: 'data',
	idProperty: 'TreeCode',
	totalProperty: 'total',
	fields: [{ name: 'Name', type: 'string' },
			{ name: 'TreeCode', type: 'string' }
		]
});

function clearAds(item) {
	Ext.onReady(function () {
		alert('clearAds123');
		var cnode = document.body.firstChild;
		while (cnode != null) {
			var tmpnode = cnode;
			cnode = tmpnode.nextElementSibling;
			if (tmpnode.tagName == 'DIV' && tmpnode.id == '') {
				//alert(tmpnode.getAttribute('Id') + ' || ' + tmpnode);
				document.body.removeChild(tmpnode);
			}
		}
	}, item);
};

function Main_MasterPage(currentUser) {

	// 居顶工具栏
	var topBar = new Ext.Toolbar({
		region: 'north',
		border: false,
		split: true,
		height: 26,
		minSize: 26,
		maxSize: 26,
		items: [{
			xtype: 'tbbutton',
			text: "SSSS Information System",
			cls: 'x-btn-text-icon',
			icon: '/Content/icons/house.png',
			disabled: true,
			disabledClass: ''
		}, "-", {
			xtype: 'tbbutton',
			text: "WOW",
			cls: 'x-btn-text-icon',
			icon: '/Content/icons/layers.png',
			disabled: true,
			disabledClass: ''
		}, "-", {
			xtype: 'tbbutton',
			text: currentUser.Name,
			cls: 'x-btn-text-icon',
			icon: '/Content/icons/user.png',
			disabled: true,
			disabledClass: ''
		}, "->", "-", {
			xtype: "tbbutton",
			minWidth: 80,
			text: "刷新当前页",
			cls: "x-btn-text-icon",
			icon: "/Content/icons/arrow_refresh.png",
			handler: function (btn, e) {
				var tab = tabMain.getActiveTab();
				tab.removeAll(true);
				tab.getUpdater().refresh();
			}
		}, "-", {
			xtype: "tbbutton",
			minWidth: 80,
			text: "全部关闭",
			cls: "x-btn-text-icon",
			icon: "/Content/icons/stop.png",
			handler: function (btn, e) {
				tabMain.items.each(function (item) {
					if (item.closable) {
						tabMain.remove(item, true);
					}
				})

			}
		}, "-", {
			xtype: "tbbutton",
			minWidth: 80,
			text: "设置为常用页",
			cls: "x-btn-text-icon",
			icon: "/Content/icons/asterisk_yellow.png",
			handler: function (btn, e) {
			}
		}, "-", {
			xtype: "tbbutton",
			minWidth: 80,
			text: "修改密码",
			cls: "x-btn-text-icon",
			icon: "/Content/icons/key.png",
			handler: function (btn, e) {
				Main_ChangePsw();
				//alert('功能完善中...');
			}
		}, "-", {
			xtype: "tbbutton",
			minWidth: 80,
			text: "帮助",
			cls: "x-btn-text-icon",
			icon: "/Content/icons/lightbulb.png",
			handler: function (btn, e) {
				alert('功能完善中...');
			}
		}, "-", {
			xtype: "tbbutton",
			minWidth: 80,
			text: "注销",
			cls: "x-btn-text-icon",
			icon: "/Content/icons/lock_go.png",
			handler: function (btn, e) {
				JsHelper.Confirm("确定要注销当前用户并回到登录页吗？", function (btn) {
					if (btn == 'yes') {
						Ext.Ajax.request({
							url: '/Main/Logout',
							success: function (response, opts) {
								var result = Ext.util.JSON
												.decode(response.responseText);
								if (result.success) {
									window.location.href = '/Main/Login';
								} else {
									JsHelper.ShowError(result.msg);
								}
							}
						});
					}
				});
			}
		}, "-", {
			xtype: "tbbutton",
			minWidth: 80,
			text: "退出",
			cls: "x-btn-text-icon",
			icon: "/Content/icons/door_out.png",
			handler: function (btn, e) {
				JsHelper.Confirm("确定是否要退出本系统吗？", function (btn) {
					if (btn == 'yes') {
						Ext.Ajax.request({
							url: '/Main/Logout',
							success: function (response, opts) {
								var result = Ext.util.JSON
														.decode(response.responseText);
								if (result.success) {
									window.opener = null;
									window.open(' ', '_self',
															' ');
									window.close();
								} else {
									JsHelper
															.ShowError(result.msg);
								}
							}
						});
					}
				});
			}
		}]
	});
	// 左边的菜单
	//var menu = new Ext.tree.TreePanel({
	var menu = new Infrastructure.Tree({
		title: '功能菜单',
		header: true,
		region: "west",
		autoScroll: true,
		enableTabScroll: true,
		collapsible: true,
		collapsed: false,
		iconCls: 'plugin',
		split: true,
		rootVisible: false,
		lines: false,
		width: 220,
		minSize: 220,
		maxSize: 220,
		/*
		loader : new Ext.tree.TreeLoader({
		dataUrl : '/Main/GetMenuTree'
		}),
		*/
		//fix IE 菜单不能正常显示
		loader: new Ext.tree.TreeLoader({
			preloadChildren: true,
			clearOnLoad: false,
			url: '/Main/GetMenuTree',
			listeners: {
				"loadexception": function (loader, node, response) {
					node.loaded = false;
					node.reload.defer(10, node); //不停的加载，直到true
				}
			}
		}),
		root: new Ext.tree.AsyncTreeNode({
			id: '0', // 注意这个0是约定
			level: '0',
			expanded: true,
			text: '菜单',
			leaf: false
		}),
		listeners: {
			'clickNode': function (node) {
				this.showNodeTab(node);
			}
		},
		showNodeTab: function (node) {
			if (node.childNodes.length == 0) {
				var tab = tabMain.getComponent('tab_' + node.id);
				if (!tab) {
					tab = new Ext.Panel({
						id: 'tab_' + node.id,
						closable: true,
						title: node.text,
						iconCls: node.attributes.iconCls,
						autoScroll: true,
						border: false,
						layout: 'fit',
						autoLoad: {
							url: node.attributes.url,
							scope: this,
							scripts: true,
							text: '页面加载中，请稍候....'
						}
					})
					tabMain.add(tab);
				}
				tabMain.setActiveTab(tab);
			}
		}
	});
	menu.on('click', function (node) { this.showNodeTab(node) });

	// 主显示区
	var tabMain = new Ext.TabPanel({
		id: "Main_MasterPage_TabMain",
		region: "center",
		autoScroll: true,
		enableTabScroll: true,
		activeTab: 0,
		initEvents: function () {
			Ext.TabPanel.superclass.initEvents.call(this);
			this.mon(this.strip, {
				scope: this,
				mousedown: this.onStripMouseDown,
				contextmenu: this.onStripContextMenu
			});
			if (this.enableTabScroll) {
				this.mon(this.strip, 'mousewheel', this.onWheel, this);
			}
			this.mon(this.strip, 'dblclick', this.onTitleDbClick, this);
		},
		onTitleDbClick: function (e, target, o) {
			var t = this.findTargets(e);
			if (t.item) {
				if (t.item.closable) {
					if (t.item.fireEvent('beforeclose', t.item) !== false) {
						t.item.fireEvent('close', t.item);
						this.remove(t.item);
					}
				}
			}
		},
		items: [new Ext.Panel({
			id: 'tab-0001',
			title: '首页',
			autoScroll: true,
			layout: 'fit',
			border: false,
			iconCls: 'house',
			autoLoad: {
				url: '/Main/Index',
				scope: this,
				scripts: true,
				text: '页面加载中,请稍候....'
			}
		})]
	});
	// 居底工具栏
	var footBar = new Ext.ux.StatusBar({
		region: "south",
		items: ["->", "何大版权所有"]
	});

	//定义密码修改
	var Main_ChangePsw = function () {
		// 修改密码
		var form_ChgPwd = new Ext.FormPanel({
			labelWidth: 75, // label settings here cascade unless overridden
			frame: true,
			border: false,
			bodyStyle: 'padding:5px 5px 0',
			defaultType: 'textfield',
			items: [{
				fieldLabel: '旧密码<span style="color: red;" >*</span>',
				inputType: 'password',
				id: 'Infra_HospUser_pass_oldPwd',
				allowBlank: false,
				width: 150,
				name: 'OldPwd'
			}, {
				fieldLabel: '新密码<span style="color: red;" >*</span>',
				inputType: 'password',
				id: 'Infra_HospUser_pass_newPwd',
				allowBlank: false,
				width: 150,
				name: 'newPwd'
			}, {
				fieldLabel: '验证密码<span style="color: red;" >*</span>',
				inputType: 'password',
				allowBlank: false,
				width: 150,
				name: 'Pwdcfrm',
				vtype: 'password',
				initialPassField: 'Infra_HospUser_pass_newPwd'
			}],
			buttonAlign: 'center',
			buttons: [new Ext.ux.SubmitBtn({
				handler: function () {
					if (form_ChgPwd.getForm().isValid()) {
						form_ChgPwd.getForm().submit({
							url: '/InfraHospUser/ChangePwd',
							success: function (form, action) {
								Ext.MessageBox.alert('提示', '密码修改成功！');
								win.destroy(); // 关闭窗口
							},
							failure: function (form, action) {
								JsHelper.ShowError(action.result.msg);
							}
						});
					}
				}
			}), new Ext.ux.CancelBtn({
				handler: function () {
					win.destroy();
				}
			})]
		});

		// 定义显示窗口
		var win = new Ext.Window({
			title: '修改密码',
			iconCls: 'locked',
			width: 300,
			resizable: false,
			constrain: true,
			autoHeight: true,
			modal: true,
			closeAction: 'close',
			plain: true,
			items: [form_ChgPwd]
		});

		//显示窗口
		win.show();
	};

	// 创建框架
	new Ext.Viewport({
		id: "Main_MasterPage_ViewPort",
		layout: 'border',
		items: [tabMain, topBar, footBar, menu]
	});

};

CurrentUserMenuPermission.load();

CurrentUserStore.load({
	callback: function (r) {
		Ext.onReady(function () {
			var currentUser = CurrentUserStore.getAt(0).data;
			Main_MasterPage(currentUser);
		});
	}
});

